package cn.signalpha.modules.base.controller.wx;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.base.entity.WxReplyRule;
import cn.signalpha.modules.base.service.WxReplyRuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/wx/reply/rule")
@Api(value = "微信消息回复", tags = "微信消息回复")
public class WxReplyRuleController extends BaseController {

	private final WxReplyRuleService wxReplyRuleService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="type", dataType = "string", value = "类型"),
        @ApiImplicitParam(name="name", dataType = "string", value = "规则名"),
        @ApiImplicitParam(name="status", dataType = "string", value = "状态"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<WxReplyRule>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<WxReplyRule> wrapper = wxReplyRuleService.selectWxReplyRuleWrapper(params);
        return R.data(wxReplyRuleService.page(Condition.getPage(query), wrapper));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<WxReplyRule> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(wxReplyRuleService.getById(id));
	}

    @PostMapping("/submit")
    @ApiOperation(value = "提交", notes = "传入wxReplyRule")
    public R submit(@Valid @RequestBody WxReplyRule wxReplyRule) {
		if(wxReplyRule.getId() == null){
			wxReplyRule.setCreateTime(new Date());
		}
		wxReplyRule.setUpdateTime(new Date());
        return R.status(wxReplyRuleService.saveOrUpdate(wxReplyRule));
    }

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(wxReplyRuleService.removeByIds(Func.toLongList(ids)));
	}

}
